package UserInterfase;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.ImageIcon;

/** A collection of useful tools for manipulating the user interfaces. 
 */
public class UITools {

	/** Creates an image icon. 
	 * @param path The URL or path to the icon.
	 * @param desc The text representation of the icon. 
	 */
	public static ImageIcon createImageIcon(String path, String desc) {
		ImageIcon pic = new ImageIcon(path, desc);
		return pic;
	}

	/** Returns the centre of a screen. 
	 * @return The coordinates of the centre of the screen.
	 */
	public static Point getCenterOfScreen() {
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		Point center = new Point();
		center.setLocation(screen.width / 2, screen.height / 2);
		return center;
	}

	/** Returns the centre of a graphical Component. 
	 * @param obj Component to find the centre of.
	 * @return coordinates of the centre of the Component.
	 */
	public static Point getCenterOfComponent(Component obj) {
		Dimension compSize = obj.getSize();
		Point center = new Point();
		center.setLocation(compSize.width / 2, compSize.height / 2);
		return center;
	}

	/** Returns the upper left corner coordinate for Component, so that the
	 * Component's centre is the specified Point.  This will allow for a Component
	 * to be moved in appropriate manner over a desired point.
	 * 
	 * Random Thought:
	 * Originally thought of using a method that finds the adjustment point, and then 
	 * sets the component to the adjustment point.  However, this method might be 
	 * useful for some non-widget objects, especially when doing double buffering 
	 * images.
	 * @param obj Component whose location is to be adjusted.
	 * @param targetCenter Point that is to be the new center of the 
	 */
	public static Point getAdjustToPoint(Component obj, Point targetCenter) {
		Point sourceCenter = getCenterOfComponent(obj); // Present center.
		Point sourceCorner = obj.getLocation();
		// Top left corner of the component.

		/*This procedure is quite obvious... Just draw two points on the back of
		 * an envelope.  You will know what I mean. ;-) */
		int xShift =
			Math.abs((int) (sourceCenter.getX() - targetCenter.getX()));
		int yShift =
			Math.abs((int) (sourceCenter.getY() - targetCenter.getY()));

		/* OK... So this is a cheap addon.  Basically tells which direction the target 
		 * and source points are in relationship to each other. */
		if (targetCenter.getX() < sourceCenter.getX()) {
			xShift = -xShift;
		} else {
		}

		if (targetCenter.getY() < sourceCenter.getY()) {
			yShift = -yShift;
		} else {
		}

		Point targetCorner =
			new Point(
				(int) (sourceCorner.getX() + xShift),
				(int) (sourceCorner.getY() + yShift));
		return targetCorner;
	}
	
	/** Translates board interface coordinates into their virtual game board
	 * coordinates.  Note that the origin of the board is the top left corner
	 * of the image or interface of the board.
	 * @param boardCoords coordinates based on the image coordinate system.
	 * @param tileSize the pixel size of the each "square" or tile on the board.
	 * @return coordinates based on the board.
	 */
	public static Point getGameCoords(Point boardCoords, int tileSize) {
		int gameX = 1 + (int)(boardCoords.getX()) / tileSize;
		int gameY = 1 + (int)(boardCoords.getY()) / tileSize;
	
		return new Point(gameX, gameY);
	}
	
	/** Translates virtual game board coordinates into their board interface
	 * coordinates.  Note that the origin of the board is the top left corner
	 * of the image or interface of the board. 
	 * @param boardCoords coordinates based on the image coordinate system.
	 * @param tileSize the pixel size of the each "square" or tile on the board.
	 * @return coordinates of the top left corner of the tile on the real image.
	 */
	public static Point getBoardCoords(Point gameCoords, int tileSize) {
		int boardX = ((int)(gameCoords.getX()) - 1) * tileSize;
		int boardY = ((int)(gameCoords.getY()) - 1) * tileSize;
	
		return new Point(boardX, boardY);
	}
	
	/** Translates window coordinates into their board interface coordinates.
	 * Note that the origin of the board is the top left corner
	 * of the image or interface of the board. 
	 * @param windowCoords coordinates based on the window coordinate system.
	 * @param boardOrigin the origin of the board on the window.
	 * @return coordinates relative to the board interface.
	 */
	public static Point getBoardCoords(Point windowCoords, Point boardOrigin) {
		// TODO: implement method.
		return null;
	}
	
	/** Translates board interface coordinates into their real window coordinate
	 * system equivalents.  Note the the origin of the board is the top left corner of the
	 * image or interface of the board.
	 * @param boardCoords coordinates based on the board interface coordinate system.
	 * @param boardOrigin the origin of the board on the window.
	 * @return coordinates relative  to the window.
	 */
	public static Point getWindowCoords(Point boardCoords, Point boardOrigin) {
		// TODO: implement method.
		return null;
	}
}
